package com.zretc.controller;

import com.aliyuncs.exceptions.ClientException;

import com.zretc.entity.User;
import com.zretc.service.TrackService;
import com.zretc.service.UserService;
import com.zretc.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class FrontRegisterController {
    @Autowired
    private UserService userService;
    private static final Logger logger = LoggerFactory.getLogger(FrontRegisterController.class);

    @RequestMapping("/number")
    @ResponseBody
    public Boolean getCheckNumber(String phone, HttpSession session) {
        String s = "";
        try {
            s = AliSMSUtil.sendCode(phone);
            logger.debug(s);
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
        session.setAttribute("session", s);
        return true;
    }

    @RequestMapping("/numberCheck")
    @ResponseBody
    public Boolean numberCheck(String numberCheck, HttpSession session) {
        if (session.getAttribute("session") == null) {
            return false;
        } else {
            String session1 = session.getAttribute("session").toString();
            if (session1.equals(numberCheck)) {
                return true;
            } else {
                return false;
            }
        }
    }

    @RequestMapping("/name")
    @ResponseBody
    public String name(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("userName", name);
        User userByCondition = userService.findUserByCondition(map);

        String flag = "";

        if (userByCondition != null) {
            flag = "false";
        } else {
            flag = "true";
        }

        return flag;
    }

    @RequestMapping("/phone")
    @ResponseBody
    public Boolean phone(String number) {
        Map<String, Object> map = new HashMap<>();
        map.put("userPhone", number);
        User userByCondition = userService.findUserByCondition(map);
        if (userByCondition != null) {
            return false;
        } else {
            return true;
        }
    }

    @RequestMapping("/send")
    @ResponseBody
    public Boolean send(String registName, String phoneNumber, String numberCheck, String confirmPsw, MultipartFile checkpicture) throws IOException {
        String fileSuffix = "";
        User user = new User();
        user.setUserId(Util.getUUID());
        user.setUserName(registName);
        user.setUserPhone(phoneNumber);
        user.setUserPsw(Util.getMd5(confirmPsw));
        if (userService.insertUser(user) == 1) {
            if (checkpicture.getSize() != 0) {
                // 获取文件名
                String filename = checkpicture.getOriginalFilename();
                // 自定义文件名 + 文件后缀名
                fileSuffix = filename.substring(filename.lastIndexOf("."));
                // 文件存储在指定的服务器中
                File file = new File("D:/EDS/images/BaiDuFace/" + phoneNumber + fileSuffix);
                checkpicture.transferTo(file);
                byte[] bytes = FileUtil.readFileByBytes("D:/EDS/images/BaiDuFace/" + phoneNumber + fileSuffix);
                final String encode = Base64Util.encode(bytes);
                BaiDuFace.add(encode, user);
            }
            return true;
        } else {
            return false;
        }
    }

}


